import java.util.*;

/**
 * @author zhangshuai
 * @version V1.0
 * @Project Java2_26
 * @Package PACKAGE_NAME
 * @Description:
 * @Date 2025/5/16 11:12
 */

public class MapTest {
    public static void main(String[] args) {
        //Map 是一个接口，不能实例化，只能实例化它的实现类
        //HashMap 是 Map 的实现类，底层是哈希表
        //LinkedHashMap 是 Map 的实现类，底层是哈希表 + 链表
        //TreeMap 是 Map 的实现类，底层是红黑树
        //Hashtable 是 Map 的实现类，底层是哈希表
        //Properties 是 Map 的实现类，底层是哈希表
        //ConcurrentHashMap 是 Map 的实现类，底层是哈希表 + 链表 + 红黑树
            //key的类型是Object，value的类型是Object
        Map<Integer, Object> hashMap = new HashMap<>();
        //key 是 学号 value 是学生对象
        Student student = new Student("zl", 21);
        hashMap.put(1001, new Student("zs", 18));
        hashMap.put(1006, new Student("ls", 19));
        hashMap.put(1003, null);
        hashMap.put(null, new Student("zl", 41));
        hashMap.put(null, student);
        System.out.println(hashMap);
        //根据key获取value
        /*System.out.println("value="+hashMap.get(1001));
        hashMap.remove(1002);
        System.out.println(hashMap);
        //是否宝航key，是否包含value true false
        System.out.println(hashMap.containsKey(1006));
        System.out.println(hashMap.containsValue(student));//true
        System.out.println(hashMap.isEmpty());//是否为空 true false
        System.out.println(hashMap.size());//键值对的个数
        hashMap.clear();//清空
        //遍历
       hashMap.forEach((key,value)->{
           System.out.println("key="+key+" value="+value);
       });

       //遍历keySet
        Set<Integer> integers = hashMap.keySet();
        for (Integer integer : integers) {
            System.out.println("keySet="+integer + " value="+hashMap.get(integer));
        }
        //遍历values
        Collection<Object> values = hashMap.values();
        for (Object value : values) {
            System.out.println("values="+value);
        }
        //遍历entrySet  键值对的Set集合
        Set<Map.Entry<Integer, Object>> entries = hashMap.entrySet();
        for (Map.Entry<Integer, Object> entry : entries) {

            System.out.println("entrySet="+entry.getKey()+" value="+entry.getValue());
        }
        Iterator<Map.Entry<Integer, Object>> iterator = entries.iterator();
        while (iterator.hasNext()) {
            //next()方法返回的是一个键值对的Entry对象
            Map.Entry<Integer, Object> next = iterator.next();

            System.out.println("entrySet="+next.getKey()+" value="+next.getValue());
        }*/
        //hashMap 是 Map 的实现类，底层是哈希表   哈希表的底层是数组 + 链表 + 红黑树
        //LinkedHashMap 是 Map 的实现类，底层是哈希表 + 链表
        Map<Object, Object> linkedHashMap = new LinkedHashMap<>();
        Hashtable<Object, Object> hashtable = new Hashtable<>();
        TreeMap<String, Object> treeMap = new TreeMap<>();
        //Properties 是 Map 的实现类，底层是哈希表，key和value都是String类型，一般是去写配置文件
        Properties properties = new Properties();
        //linkedHashMap
        linkedHashMap.put(1001, new Student("zs", 18));
        linkedHashMap.put(1001, new Student("zs", 30));
        linkedHashMap.put(1006, new Student("ls", 19));
        linkedHashMap.put(null, null);
        linkedHashMap.put(1008, new Student("zl", 21));
        System.out.println("linkedHashMap"+linkedHashMap);

        //hashtable
        hashtable.put(1001, new Student("zs", 18));
        hashtable.put(1001, new Student("zs", 30));
        hashtable.put(1002, new Student("ls", 19));
        hashtable.put(1003, new Student("zsl", 21));
        System.out.println("hashtable"+hashtable);

        //treeMap  底层是红黑树  key 是自然排序 升序排列
        //自然排序 适用与基本类型和String类型
        treeMap.put("aa", new Student("zs", 18));
        treeMap.put("ca", new Student("zs", 30));
        treeMap.put("ba", new Student("zs", 30));
        treeMap.put("daaaa", new Student("ls", 19));
        treeMap.put("dc", new Student("zsl", 21));

        System.out.println("treeMap"+treeMap);





    }
}
class Student{
    private String name;
    private int age;

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}